//
//  EditNameViewController.m
//  ChatKit-OC
//
//  Created by th on 2017/3/29.
//  Copyright © 2017年 ElonChan. All rights reserved.
//

#import "EditNameViewController.h"

@interface EditNameViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userNameTF;


@end

@implementation EditNameViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"EditNameStoryboard" bundle:[NSBundle mainBundle]];
        self = [sb instantiateInitialViewController];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.title = @"修改昵称";
    
    if (_userName.length > 0) {
        self.userNameTF.text = _userName;
    }
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(editName)];
    
    [self.userNameTF becomeFirstResponder];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.userNameTF resignFirstResponder];
}

- (void)editName {
    
    if (_userNameTF.text.length == 0) {
        [LCCKUtil showNotificationWithTitle:@"请输入昵称" subtitle:nil type:LCCKMessageNotificationTypeError];
    } else if ([_userNameTF.text isEqualToString:_userName]) {
        [LCCKUtil showNotificationWithTitle:@"请更改不同的昵称哦 " subtitle:nil type:LCCKMessageNotificationTypeError];
    } else {
        
        AVQuery *query = [AVQuery queryWithClassName:kUser];
        
        [query whereKey:LCCKProfileKeyName equalTo:_userNameTF.text];//精确查询
        
        MJWeakSelf;
        [query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
            
            if (!error) {
                if (objects.count > 0) {
                    //有相同名字的用户
                    [LCCKUtil showNotificationWithTitle:@"当前用户名已注册" subtitle:nil type:LCCKMessageNotificationTypeError];
                } else {
                    
                    if (weakSelf.editNameBlock) {
                        weakSelf.editNameBlock(_userNameTF.text);
                        
                        [weakSelf.navigationController popViewControllerAnimated:YES];
                    }
                }
            } else {
                [LCCKUtil showNotificationWithTitle:[error localizedDescription] subtitle:nil type:LCCKMessageNotificationTypeError];
            }
        }];
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
